/*
 * Denne klasse skal bruges til at gemme og finde alle oplysningerne
 * omkring items i databasen.
 */
package db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import models.Storage;

/**
 *
 * @author Dan
 */
public class StorageDb extends AbstractDb implements StorageInterface {

    public int addItem(Storage sto) {
        int lastId = 0;
        try { // insert new Storage
            int rc = this.db.queryInsert("INSERT INTO Lif_Storage (storage_description, storage_name, storage_amount)  VALUES("
                    + "'" + sto.getDescription() + "',"
                    + "'" + sto.getName() + "',"
                    + "'" + sto.getAmount() + "');");
            if (rc > 0) {
                lastId = this.db.getInsertId();
            }
        }//end try
        catch (Exception ex) {
            System.out.println("Item was not created");
            System.out.println("Insert exception in storage db: " + ex);

        }
        return (lastId);
    }

    public boolean editItem(Storage sto) {
        Storage stoObj = sto;
        boolean succes = false;
        try { // Update supplier
            int rc = this.db.queryUpdate("UPDATE Lif_Storage SET "
                    + "storage_description='" + stoObj.getDescription() + "', "
                    + "storage_name='" + stoObj.getName() + "', "
                    + "storage_amount='" + stoObj.getAmount() + "' WHERE storage_id =" + stoObj.getId() + ";");
            if (rc > 0) {
                succes = true;
            }
        }//end try
        catch (Exception ex) {
            System.out.println("Item was not Update");
            System.out.println("Update exception in storage item db: " + ex);
        }
        return succes;
    }

    public boolean deleteItem(int id) {
        int rc = -1;
        boolean succeed = false;
        try { // delete from supplier
            String query = "DELETE FROM Lif_Storage "
                    + "WHERE storage_id = '" + id + "'";
            rc = this.db.queryUpdate(query);
            //end try
            if (rc > -1) {
                succeed = true;
            }
        } catch (Exception ex) {
            System.out.println("storage db Delete exception: " + ex);
        }
        return (succeed);
    }

    public ArrayList<Storage> searchItem() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Storage getStorage(int Id) {
        Storage stoObj = null;

        ResultSet rs = this.db.query("SELECT * FROM Lif_Storage WHERE storage_id=" + Id);
        try {
            if (rs != null && rs.next()) {
                String name = rs.getString("storage_name");
                String description = rs.getString("storage_description");
                int amount = rs.getInt("storage_amount");
                stoObj = new Storage(name, description, amount);
            }
        } catch (SQLException e) {
            stoObj = null;
        }
        return stoObj;
    }

    public ArrayList<Storage> getAllItems() {
        ArrayList<Storage> storageList = new ArrayList<Storage>();
        try {
            ResultSet items = this.db.query("SELECT * FROM Lif_Storage");
            if (items != null) {
                while (items.next()) {
                    Storage storage = new Storage(
                            items.getInt("storage_id"),
                            items.getString("storage_name"),
                            items.getString("storage_description"),
                            items.getInt("storage_amount"));
                    storageList.add(storage);
                }
            }
        } catch (Exception e) {
            System.out.println("Der var en fejl så tingene blev ikke hentet.");
        }
        return storageList;

    }
}
